import { login, logout, profile } from '@/api/base/frame'
import { getToken, setToken, removeToken, setTimeStamp } from '@/utils/auth'

const user = {
  state: {
    user: '',
    status: '',
    code: '',
    token: getToken(),
    name: '',
    avatar: '',
    introduction: '',
    roles: [],
    setting: {
      articlePlatform: []
    }
  },

  mutations: {
    SET_CODE: (state, code) => {
      state.code = code
    },
    SET_TOKEN: (state, token) => {
      state.token = token
    },
    SET_INTRODUCTION: (state, introduction) => {
      state.introduction = introduction
    },
    SET_SETTING: (state, setting) => {
      state.setting = setting
    },
    SET_STATUS: (state, status) => {
      state.status = status
    },
    SET_NAME: (state, name) => {
      state.name = name
    },
    SET_AVATAR: (state, avatar) => {
      state.avatar = avatar || ''
    },
    SET_ROLES: (state, roles) => {
      state.roles = roles
    }
  },

  actions: {
    // 用户名登录
    async LoginByUsername ({ commit }, userInfo) {
      try {
        const username = userInfo.username.trim()
        const res = await login({
          username: username,
          password: userInfo.password
        })
        // console.log(res)
        commit('SET_TOKEN', res.data.token)
        setToken(res.data.token)
        setTimeStamp()
        // this.$message.success('登录成功')
      } catch (e) {
        // this.$message.error('登录失败')
        console.log(e)
      }
      // this.$router.push({ path: '/' })
      // return new Promise((resolve, reject) => {
      //   login({
      //     username: username,
      //     password: userInfo.password
      //   })
      //     .then(response => {
      //       const data = response.data
      //       commit('SET_TOKEN', data.token)
      //       setToken(response.data.token)
      //       resolve()
      //     })
      //     .catch(error => {
      //       reject(error)
      //     })
      // })
    },

    // 获取用户信息
    GetUserInfo ({ commit, state }) {
      return new Promise((resolve, reject) => {
        profile()
          .then(response => {
            const data = response.data
            commit('SET_ROLES', data.roles) // 权限数组 roles
            commit('SET_NAME', data.name)
            commit('SET_AVATAR', data.avatar)
            commit('SET_INTRODUCTION', data.introduction)
            resolve(response)
          })
          .catch(error => {
            reject(error)
          })
      })
    },

    // 第三方验证登录
    // LoginByThirdparty({ commit, state }, code) {
    //   return new Promise((resolve, reject) => {
    //     commit('SET_CODE', code)
    //     loginByThirdparty(state.status, state.email, state.code).then(response => {
    //       commit('SET_TOKEN', response.data.token)
    //       setToken(response.data.token)
    //       resolve()
    //     }).catch(error => {
    //       reject(error)
    //     })
    //   })
    // },

    // 登出
    LogOut ({ commit, state }) {
      return new Promise((resolve, reject) => {
        logout()
          .then(() => {
            commit('SET_TOKEN', '')
            commit('SET_ROLES', [])
            removeToken()
            resolve()
          })
          .catch(error => {
            reject(error)
          })
      })
    },

    // 前端 登出
    FedLogOut ({ commit }) {
      return new Promise(resolve => {
        commit('SET_TOKEN', '')
        removeToken()
        resolve()
      })
    }
  }
}

export default user
